package com.c.ctgproject.ui.files

import androidx.lifecycle.ViewModelProvider
import com.c.ctgproject.R
import com.c.ctgproject.base.BaseActivity
import com.c.ctgproject.mode.MyConstants
import com.c.ctgproject.utils.*
import kotlinx.android.synthetic.main.activity_change_car_info.*

/**
 *作者:LiBW
 *创建日期: 2021/6/24
 *描述:修改车辆信息
 */
class ChangeCarInfoActivity : BaseActivity() {
    private val mViewModel by lazy { ViewModelProvider(this)[ChangeCarInfoViewModel::class.java] }
    private val carId by lazy { intent.getStringExtra(MyConstants.CAR_ID).handlerNull() }
    override fun getLayout() = R.layout.activity_change_car_info

    override fun event() {
        mNameET.textSet(intent.getStringExtra(MyConstants.CAR_OWNER).handlerNull())
        mCarPlateET.textSet(intent.getStringExtra(MyConstants.CAR_PLATE).handlerNull())
        mCarVinET.textSet(intent.getStringExtra(MyConstants.CAR_VIN).handlerNull())

        mViewModel.result.observe(this, {
            MyLog.d("修改回调:$it")
            handlerHttpResult(it) {
                showToast("修改成功")
                finish()
            }
        })
        mViewModel.error.observe(this, {
            showToast(it)
        })
    }

    override fun click() {
        mConfirmBTN.setOnClickListener {
            val name = mNameET.text.handlerNull()
            val plate = mCarPlateET.text.handlerNull()
            val vin = mCarVinET.text.handlerNull()
            when {
                name.isEmpty() -> showToast("请输入车主姓名")
                plate.isEmpty() -> showToast("请输入车牌号")
                vin.isEmpty() -> showToast("请输入车架号")
                else -> mViewModel.changeCarInfo(carId, name, plate, vin)
            }
        }
    }
}